# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=pastix
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.2.1
pkgrel=3
pkgdesc='High performance parallel solver for very large sparse linear systems based on direct methods (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-hwloc"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-scotch"
	 "${MINGW_PACKAGE_PREFIX}-starpu"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc")
provides=("${MINGW_PACKAGE_PREFIX}-spm")
license=('LGPL')
url="https://gitlab.inria.fr/solverstack/pastix"
source=("https://gitlab.inria.fr/solverstack/pastix//uploads/3fc798eb3ca6282e21506349df7f9da2/${_realname}-${pkgver}.tar.gz")
sha256sums=('B680CBFC265DF8CBA18D3A7093FCC02E260198C4A2D6A86D1E684BB291E309DD')

build() {
  local _arch_opt=""
  if [ "${CARCH}" = "i686" ]; then
    _arch_opt="-DBUILD_64bits=OFF"
  fi

  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DPASTIX_INT64=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DPASTIX_WITH_PARSEC=OFF \
      -DPASTIX_WITH_STARPU=ON \
      -DPASTIX_ORDERING_METIS=ON \
      -DPASTIX_WITH_MPI=OFF \
      -DBUILD_DOCUMENTATION=OFF \
      -DBUILD_TESTING=OFF \
      ${_arch_opt} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DPASTIX_INT64=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DPASTIX_WITH_PARSEC=OFF \
      -DPASTIX_WITH_STARPU=ON \
      -DPASTIX_ORDERING_PTSCOTCH=ON \
      -DPASTIX_ORDERING_METIS=ON \
      -DPASTIX_WITH_MPI=OFF \
      -DBUILD_DOCUMENTATION=OFF \
      -DBUILD_TESTING=OFF \
      -DBUILD_SHARED_LIBS=ON \
      ${_arch_opt} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install

  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE  
  
  #Remove unwanted examples files
  #rm -rf ${pkgdir}${MINGW_PREFIX}/examples
}
